package com.jinchengll.xigua.ui.viewmodel;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.jinchengll.xigua.entity.Page;
import com.jinchengll.xigua.entity.Video;
import com.jinchengll.xigua.network.base.BaseObserver;
import com.jinchengll.xigua.util.ToastUtil;

import java.util.List;

/**
 * @author jinchengll
 * @since 2021/10/3
 */
public class MainViewModel extends BaseViewModel {

    private final String TAG = "MainViewModel";

    private int pageNo = 1;
    private int pageSize = 5;

    public MutableLiveData<String> textViewLD = new MutableLiveData<>();


    public void getViewPage() {
        repository.getVideoPage(pageNo, pageSize, new BaseObserver<Page<Video>>() {
            @Override
            public void onSuccess(Page<Video> data) {
                List<Video> records = data.getRecords();
                if (records== null || records.size() == 0) {
                    ToastUtil.showShort("没有更多数据");
                    return;
                }
                StringBuilder sb = new StringBuilder();
                for (Video record : data.getRecords()) {
                    sb.append(record.toString());
                    sb.append("/n");
                }
                textViewLD.setValue(sb.toString());
                pageNo = data.getCurrent() + 1;
            }

            @Override
            public void onFailure(String errorMsg) {
                Log.e(TAG, errorMsg);
                ToastUtil.showShort(errorMsg);
            }
        });
    }

}
